package com.itcast.demo.remoterfc.resttemplate;

import com.alibaba.fastjson.JSON;
import com.itcast.demo.remoterfc.httpurlconnection.ProductBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;

@RestController
@RequestMapping("/resttemplate")
public class RestTemplateController {
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/dopost")
    public void dopost() throws Exception {
        String baseUrl = "";
        String basicAppToke = "";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", basicAppToke);
        headers.setContentType(MediaType.APPLICATION_JSON);

        HashMap<String, String> map = new HashMap<>();
        map.put("network_id", "N10667");
        String requestBody = JSON.toJSONString(map);

        HttpEntity<String> httpEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<String> response = restTemplate
                .exchange(baseUrl, HttpMethod.POST, httpEntity, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            String responseBody = response.getBody();
            System.out.println("响应内容： " + responseBody);
        } else {
            System.out.println("请求失败：，状态码： " + response.getStatusCode());
        }
    }


    @PostMapping("/doGet")
    public void doGet() throws Exception {
        String baseUrl = "";
        String basicAppToke = "";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", basicAppToke);

        ProductBean productBean = new ProductBean();
        productBean.setOrder_id(new String[]{"123456"});
        String strs = JSON.toJSONString(productBean);

        LinkedMultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("pageSize", "5");
        multiValueMap.add("pageIdex", "1");
        multiValueMap.add("params", strs);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
        URI uri = builder.queryParams(multiValueMap).build().encode().toUri();

        HttpEntity<String> httpEntity = new HttpEntity<>(headers);


        ResponseEntity<String> response = restTemplate
                .exchange(uri, HttpMethod.GET, httpEntity, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            String responseBody = response.getBody();
            System.out.println("响应内容： " + responseBody);
        } else {
            System.out.println("请求失败：，状态码： " + response.getStatusCode());
        }
    }
}
